<?
function drizzo_get_the_comments($post_id = null, $comment_parent = 0, $level = 0, $sort_order = "DESC") {
	global $drizzo_users, $drizzo_comments, $the_post;
		
	drizzo_db_connect();
	
	$comment_query = @mysql_query("SELECT * FROM `$drizzo_comments` WHERE `post_id`='$post_id' AND `status`='approved' AND `parent_id`='$comment_parent' ORDER BY `ID` $sort_order");
	
	if(!mysql_errno()) {
		while($row = @mysql_fetch_assoc($comment_query)) {
			foreach($row as $field => $value) {
				if($field != '') {
					$comments[$i][$field] = drizzo_db_unwrap($value);
				}
			}
			if($row['author_id'] != 0) {
				$comments[$i]['author'] = drizzo_user_info($row['author_id'],'nice_name');
				$comments[$i]['author_url'] = drizzo_user_info($row['author_id'],'url');
				$comments[$i]['author_email'] = drizzo_user_info($row['author_id'],'email');
			}
			$i++;
		}
		return $the_comments;
	} else {
		return false;
	}
}

function drizzo_get_the_comment($comment_id = null) {
	global $drizzo_comments;
		
	drizzo_db_connect();
	
	$comment_query = @mysql_query("SELECT * FROM `$drizzo_comments` WHERE `ID`='$comment_id'");

	if(!mysql_errno()) {
		while($row = @mysql_fetch_assoc($comment_query)) {
			foreach($row as $field => $value) {
				if($field != '') {
					$comment[$field] = drizzo_db_unwrap($value);
				}
			}
			if($row['author_id'] != 0) {
				$comment['author'] = drizzo_user_info($row['author_id'],'nice_name');
				$comment['author_url'] = drizzo_user_info($row['author_id'],'url');
				$comments['author_email'] = drizzo_user_info($row['author_id'],'email');
			}
		}
		return $the_comment;
	} else {
		return false;
	}
}

function drizzo_the_comments($post_id = null, $style = "ol", $ajax = false) {
	global $the_post;
	
	if($the_post) {
		$post_id = $the_post['ID'];
	}
	$no = 0;
	$comments = drizzo_get_the_comments($post_id);

	if($comments) {
		if($style == "ul" || $style == "ol") { // Comments are a standard list
			$comments_string .= "<$style id=\"comments_list\">\n";
			foreach($comments as $comment) {
				$level = $comment['level'];
				if($the_post['author'] == $comment['author_id']) {
					$comments_string .= "\t<li class=\"comment author\" style=\"padding-left: " . $level . "00%;\"><a name=\"comment-" . $comment['ID'] . "\"></a>";
				} else {
					$comments_string .= "\t<li class=\"comment\" style=\"padding-left: " . $level . "00%;\"><a name=\"comment-" . $comment['ID'] . "\"></a>";
				}
				$gravatar_url = "http://www.gravatar.com/avatar.php?gravatar_id=" . $comment['author_email'];
				$comments_string .= "<span class=\"avatar\">";
				if(drizzo_user_info($comment['author_id'],'avatar')) {
					$user_avatar_url = admin_folder . "engine/user_avatar.php?user_id=" . $comment['author_id'];
					if($comment['author_url']) {
						$comments_string .= "<a href=\"" . $comment['author_url'] . "\">";
					}
					$comments_string .= "<img src=\"" . $user_avatar_url . "\" alt=\"Avatar\" class=\"avatar_img\" width=\"40\" height=\"40\" /><br />";
					$comments_string .= $comment['author'];
					if($comment['author_url']) {
						$comments_string .= "</a>";
					}
					$comments_string .= "</span>";
				} elseif(file_exists($gravatar_url)) {
					if($comment['author_url']) {
						$comments_string .= "<a href=\"" . $comment['author_url'] . "\">";
					}
					$comments_string .= "<img src=\"" . $gravatar_url . "\" alt=\"Gravatar\" class=\"avatar_img\" /><br />";
					$comments_string .= $comment['author'];
					if($comment['author_url']) {
						$comments_string .= "</a>";
					}
				} else {
					$comments_string .= $comment['author'];
				}
				$comments_string .= "</span>";
				if($comment['title'] != null) {
					$comments_string .= "<h2 class=\"comment_title\">" . $comment['title'] . "</h2>\n";
				}
				$comments_string .= "<div class=\"comment_content\">" . $comment['content'] . "</div>";
				$comments_string .= "<span class=\"comment_date\">Posted on " . gmdate("M d, Y, g:i A", $comment['date']) . "</span>";
				$comments_string .= drizzo_edit_link("<span class=\"edit_link\">","</span>");
				$comments_string .= "<br class=\"clear\" /></li>\n";
			}
				$comments_string .= "</$style>\n";
		}
	} else {
		$comments_string = false;
	}
	if(!$_POST) {
		if($ajax) {
			$comments_string .= "<h2><a href=\"javascript:void(0)\" onclick=\"new Ajax.Updater('comment_box', '" . domain . "drizzo/includes/comment_box.php?ajax=true&post_id=" . $post_id . "', {onComplete:function(){ new Effect.Appear('comment_box');},asynchronous:true, evalScripts:true})\" title=\"Add a Comment\">Add a Comment</a></h2>\n";
			$comments_string .= "<div id=\"comment_box\" style=\"display: none;\"></div>\n";
		} else {
			$comments_string .= "<h2>Add a Comment</h2>\n";
			$comments_string .= "<div id=\"comment_box\">";
			$comment_box_uri  = domain . "drizzo/includes/comment_box.php?post_id=" . $post_id;
			$comments_string .= file_get_contents($comment_box_uri);
			$comments_string .= "</div>\n";
		}
	}
	return $comments_string;	
}

function drizzo_comment_save() {
	global $drizzo_comments;
	$_POST = drizzo_db_wrap($_POST);
	extract($_POST);
	$date = time();
	$comment_insert = @mysql_query("INSERT INTO `$drizzo_comments` (`post_id`,`date`,`content`,`title`,`author_id`,`author_name`,`author_email`,`author_url`,`author_ip`) VALUES('$post_id','$date','$content','$title','$author_id','$author_name','$author_email','$author_url','$author_ip')");
	if(!mysql_errno()) {
		drizzo_cache_destroy(drizzo_post_permalink($post_id));
		return true;
	} else {
		return false;
	}
}

function drizzo_comment_count($id = null) {
	global $the_post, $drizzo_comments;
	
	if($the_post && !$id) {
		return $the_post['comment_count'];
	} elseif(!$id) {
		return false;
	} else {
		
		drizzo_db_connect();

		$comment_count = '0';

		$comments_query = @mysql_query("SELECT `post_id` FROM `$drizzo_comments` WHERE `post_id`='$id' AND `status`='approved'");
		if(!mysql_errno()) {
			$comment_count = mysql_num_rows($comments_query);				
		}
		return strval($comment_count);
	}

}

function drizzo_comment_status() {
	global $the_post;
	
	$comment_status = "";
	
	if(!$the_post) {
		return false;
	} else {
		$comment_status = $the_post['comment_status'];
		return $comment_status;
	}
}

function drizzo_comments_count($status) {
	global $drizzo_comments;
	if($status == 'all') {
		$comment_rows = @mysql_query("SELECT `ID` FROM `$drizzo_comments`");
		if(!mysql_errno()) {
			$count = @mysql_num_rows($comment_rows);
		} else {
			$count = 0;
		}
	} else {
		$comment_rows = @mysql_query("SELECT `ID` FROM `$drizzo_comments` WHERE `status` LIKE '%$status%'");
		if(!mysql_errno()) {
			$count = @mysql_num_rows($comment_rows);
		} else {
			$count = 0;
		}
	}
	return $count;
}

function drizzo_comment_post_info($post_id = null) {
	global $drizzo_posts;
	if($post_id) {
		$post_info_lookup = @mysql_query("SELECT `title`,`permalink_name`,`type` FROM `$drizzo_posts` WHERE `ID`='$post_id'");
		if(!mysql_errno()) {
			$post_info = @mysql_fetch_assoc($post_info_lookup);
			if($post_info['type'] == 'page') {
				$post_info['permalink'] = "/" . $post_info['permalink_name'];
			} elseif($post_info['type'] == 'image') {
				$post_info['permalink'] = drizzo_gallery_image_permalink($post_id);
			} else {
				$post_info['permalink'] = "/" . $post_info['type'] . "/" . $post_info['permalink_name'];
			}
		} else {
			$post_info = false;
		}
	} else {
		$post_info = false;
	}
	return $post_info;
}

function drizzo_comment_list($status = 'all', $time_start = false, $time_end = false, $limit = false, $offset = false, $sort = false, $direction) {
	global $drizzo_comments;
	$status = str_replace('view_', '', $status);
	if($status == 'recent' && !$time_start) {
		$time_start = time()-7*24*60*60;
	}
	if($status == 'recent' && !$time_end) {
		$time_end = time();
	}
	if($status == 'all') {
		$status = "'moderated','spam','approved'";
	} elseif($status == 'recent') {
		$status = "'moderated','approved'";
	} else {
		$status = "'" . $status . "'";
	}
	if(!$sort) {
		$sort = "ID";
	}
		$sql = "SELECT `ID`,`post_id`,`parent_id`,`date`,`author_name`,`author_email`,`status`,`title` FROM `$drizzo_comments` WHERE `status` IN ($status)";
		if($time_start && $time_end) {
			$sql .= " AND `date`>='" . $time_start . "' AND `date`<='" . $time_end . "'";
		} elseif(!$time_start && $time_end) {
			$sql .= " AND `date`<='" . $time_end . "'";
		} elseif($time_start && !$time_end) {
			$sql .= " AND `date`>='" . $time_start . "'";
		} else {
			// Do Nothing
		}
		$sql .= " ORDER BY `". $sort . "` " . $direction;
		if($offset && $limit) {
			$sql .= " LIMIT " . $offset . "," . $limit;
		}
		$comment_lookup = @mysql_query($sql);
		if(!mysql_errno()) {
			while($row = @mysql_fetch_assoc($comment_lookup)) {
				$post_info = drizzo_comment_post_info($row['post_id']);
				$row['post_title'] = $post_info['title'];
				$row['post_permalink'] = $post_info['permalink'];
				if($row['title'] == '') {
					$row['title'] == '(Untitled)';
				}
				$comments[] = $row;
			}
		} else {
			$comments = false;
		}
		return $comments;
}

function drizzo_comment_table($status = 'all', $time_start = false, $time_end = false, $limit = false, $offset = false, $sort = false) {
	global $drizzo_admin_images;
	$string = null;
	$current_url = $_SERVER['SCRIPT_NAME'] . "?";
	if($_GET['action']) { $current_url .= "action=" . $_GET['action'] . "&amp;"; }
	if($_GET['time']) { $current_url .= "time=" . $_GET['time'] . "&amp;"; }
	$direction = $_GET['direction'];
	$comments = drizzo_comment_list($status, $time_start, $time_end, $limit, $offset, $sort, $direction);
	$alt_count = 0;
	$url_args = $_SERVER['QUERY_STRING'];
	$url_args = explode('&', $url_args);
	if(count($url_args) > 0) {
		foreach($url_args as $url_arg) {
			$url_arg = explode("=", $url_arg);
			$key = $url_arg[0];
			$val = $url_arg[1];
			if($key != 'time' && $key != null) {
				$new_url_args .= "&amp;" . $key . "=" . $val;
			}
		}
	}
	$string .= "<form style=\"clear: none;float: right;\">";
	$string .= "<p class=\"right\"><label style=\"float: left;padding:0;\">Time Frame</label>";
	$string .= "<select name=\"timeframe\" id=\"timeframe\" onchange=\"location = this.options[this.selectedIndex].value;\">";
	$string .= "<option id=\"comments_menu_all\" value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=all\""; if(!$_GET['time']) { $string .= " selected"; } $string .= ">All</option>";
	$string .= "<option id=\"comments_menu_all\" value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=today\""; if($_GET['time'] == 'today') { $string .= " selected"; } $string .= ">Today</option>";
	$string .= "<option id=\"comments_menu_past_week\" value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=past_week\""; if($_GET['time'] == 'past_week' || (!$_GET['time'] && !$_GET['action'])) { $string .= " selected"; } $string .= ">Past Week</option>";
	$string .= "<option id=\"comments_menu_past_2_weeks\" value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=past_2_weeks\""; if($_GET['time'] == 'past_2_weeks') { $string .= " selected"; } $string .= ">Past 2 Weeks</option>";
	$string .= "<option id=\"comments_menu_this_month\" value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=this_month\""; if($_GET['time'] == 'this_month') { $string .= " selected"; } $string .= ">This Month</option>";
	$string .= "<option id=\"comments_menu_past_3_monts\" value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=past_3_months\""; if($_GET['time'] == 'past_3_months') { $string .= " selected"; } $string .= ">Past 3 Months</option>";
	$string .= "<option id=\"comments_menu_past_6_months\" value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=past_6_months\""; if($_GET['time'] == 'past_6_months') { $string .= " selected"; } $string .= ">Past 6 Months</option>";
	$string .= "<option id=\"comments_menu_past_12_months\" value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=past_12_months\""; if($_GET['time'] == 'past_12_months') { $string .= " selected"; } $string .= ">Past 12 Months</option>";
	$string .= "<option id=\"comments_menu_this_year\" value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=this_year\""; if($_GET['time'] == 'this_year') { $string .= " selected"; } $string .= ">This Year</option>";
	$string .= "</select>";
	$string .= "</p>";
	$string .= "</form>\n";

	$string .= "<form name=\"batch\" id=\"batch\" action=\"\" method=\"post\">\n";
	$string .= "<div id=\"table_wrap\">\n";
	$string .= "<table>\n";
	$string .= "\t<tr id=\"first\">\n";
	$string .= "\t\t<th class=\"center narrow " . drizzo_up_or_down('ID') . "\"><a href=\"" . $current_url . "sort=ID&amp;direction=" . drizzo_swap_sort_direction('ID') . "\" title=\"Sort by ID\">ID</a></th><th class=\"" . drizzo_up_or_down('title') . "\"><a href=\"" . $current_url . "sort=title&amp;direction=" . drizzo_swap_sort_direction('title') . "\" title=\"Sort by Title\">Title</a></th><th class=\"" . drizzo_up_or_down('post_id') . "\"><a href=\"" . $current_url . "sort=post_id&amp;direction=" . drizzo_swap_sort_direction('post_id') . "\" title=\"Sort by Post\">Post</a></th><th class=\"" . drizzo_up_or_down('date') . "\"><a href=\"" . $current_url . "sort=date&amp;direction=" . drizzo_swap_sort_direction('date') . "\" title=\"Sort by Date\">Date</a></th><th class=\"" . drizzo_up_or_down('author_name') . "\"><a href=\"" . $current_url . "sort=author_name&amp;direction=" . drizzo_swap_sort_direction('author_name') . "\" title=\"Sort by Author\">Author</th>";
	if($status == 'view_all' || $status == 'view_recent') {
		$string .= "<th class=\"center narrow\">Status</th>";
	}
	$string .= "<th class=\"center\" colspan=\"3\">Action</th>";
	$string .= "<th class=\"narrow\">Batch</th>\n";
	$string .= "\t</tr>\n";
	if($comments) {
		foreach($comments as $comment) {
			if ($alt_count%2) { $alt = "alt"; } else { $alt = ""; }
			$string .= "\t<tr class=\"" . $alt . "\">\n";
			$string .= "\t\t<td class=\"center\">" . $comment['ID'] . "</td><td>" . $comment['title'] . "</td><td><a href=\"" . $comment['post_permalink'] . "\" title=\"View " . $comment['post_title'] . "\">" . $comment['post_title'] . "</a></td><td>" . gmdate('M d, Y \a\t g:i A', $comment['date']) . "</td><td><a href=\"mailto:" . $comment['author_email'] . "\" title=\"Email Comment Author\">" . $comment['author_name'] . "</a></td>";
			if($status == 'view_all' || $status == 'view_recent') {
				$string .= "<td class=\"center\"><div class=\"$comment[status]\"><span>" . ucwords($comment['status']) . "</span></div></td>";
			}
			if($comment['status'] == 'approved') {
				$string .= "<td class=\"center\" width=\"1\"><a href=\"" . $comment['post_permalink'] . "#comment-" . $comment['ID'] . "\" title=\"View the Comment\" class=\"view_link\"><span>View</span></a></td>";
			} else {
				$string .= "<td class=\"center\" width=\"1\"><a href=\"comments.php?view=" . $comment['ID'] . "\" title=\"View the Comment\"><img src=\"" . $drizzo_admin_images . "view.png\" alt=\"View This Comment\" /></a></td>";
			}
			$string .= "<td class=\"center\" width=\"1\"><a href=\"comments.php?edit=" . $comment['ID'] . "\" class=\"edit_link\"><span>Edit</span></a></td><td class=\"center\" width=\"1\"><a href=\"comments.php?delete=" . $comment['ID'] . "\" class=\"delete_link\"><span>Delete</span></a></td>";
			$string .= "<td class=\"center\"><input type=\"checkbox\" name=\"batch_id[]\" value=\"" . $comment['ID'] . "\" class=\"checkbox\" /></td>\n";
			
			$string .= "\t</tr>\n";
			$alt_count++;
		}
	} else {
		$string .= "\t<tr class=\"alt\">\n";
		$string .= "\t\t<td colspan=\"10\"><br /><p class=\"alert\">No comments could be found that match the search criteria.</p></td>\n";
		$string .= "\t</tr>\n";
	}
	$string .= "</table>\n";
	$string .= "</div>\n";
	$string .= "<input type=\"hidden\" name=\"process\" id=\"process\" value=\"\" />\n";
	$string .= "<p class=\"right\">Legend: <span class=\"legend_approved\"> = Approved</span>, <span class=\"legend_moderated\"> = Moderated</span>, <span class=\"legend_spam\"> = Spam</span> &nbsp; &nbsp; <input type=\"button\" class=\"submit select\" onclick=\"SetAllCheckBoxes('batch', 'batch_id[]', '')\" value=\"Deselect All\" /> <input type=\"button\" class=\"submit select\" onclick=\"SetAllCheckBoxes('batch', 'batch_id[]', 'selected')\" value=\"Select All\" /><br />\n";
	$string .= "<input type=\"submit\" class=\"submit\" onclick=\"document.batch.process.value = 'spam'\" value=\"Mark as Spam\" /> <input type=\"submit\" class=\"submit\" onclick=\"document.batch.process.value = 'delete';return window.confirm('Are you sure you want to delete these messages?');\" value=\"Delete Comments\" /> <input type=\"submit\" class=\"submit\" onclick=\"document.batch.process.value = 'approve'\" value=\"Approve Comments\" /></p>";
	$string .= "</form>\n";

	return $string;
}

function drizzo_last_comment_date($post_id) {
	global $drizzo_comments;
	
	drizzo_db_connect();
	
	$lookup = @mysql_query("SELECT `date` FROM `$drizzo_comments` WHERE `post_id`='$post_id' ORDER BY `date` DESC LIMIT 1");
	if(!mysql_errno()) {
		$last_date = @mysql_fetch_assoc($lookup);
		$last_date = $last_date['date'];
		return $last_date;
	} else {
		return false;
	}
}
?>
